define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'sales/cost/index' + location.search,
                    add_url: 'sales/cost/add',
                    // edit_url: 'sales/cost/edit',
                    // del_url: 'sales/cost/del',
                    multi_url: 'sales/cost/multi',
                    table: 'cost',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                rowStyle:function(row, index){
                    if (row.ystatus == 4) {
                        return{
                            css:{
                                color:'red'
                            }
                        }
                    }else{
                        return{}
                    }
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class: 'sid'},
                        {field: 'number', title: __('Number')},
                        {field: 'source_number', title: __('source_number')},
                        {field: 'type', title: __('Type')},
                        {field: 'is_income', title: __('是否支出')},
                        {field: 'cost', title: __('Cost'), operate:'BETWEEN'},
                        {field: 'settle_name', title: __('结算单位'), operate:false},
                        {field: 'remark', title: __('Remark'), operate:false,class: 'remark',cellStyle:function (value, row, index) {
                                return {
                                    css: {
                                        "min-width": "100px",
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width": "120px"
                                    }
                                }
                            }},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'sourcetime', title: __('来源时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operator', title: __('Operator')},
                        {field: 'company', title: __('Company')},
                        {field: 'status', title: __('status')},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'examine',
                                    title: __('审核'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-random',
                                    hidden:function(row){
                                        if(row.ystatus != 1){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var sid =$(this).parents('tr').find('.sid').html()
                                        var url = Config.this_url+'/sales/cost/detail?ids='+sid
                                        // console.log(url)
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['80%', '90%'],
                                            title: "费用单详情",
                                            content: url,
                                            // content: Config.this_url+'/purchase/requisition',
                                        })
                                    }},
                                {
                                    name: 'printing',
                                    title: __('打印'),
                                    classname: 'btn btn-xs btn-warning btn-click editstatus',
                                    icon: 'fa fa-print',
                                    hidden:function(row){

                                    },
                                    click: function(data){
                                        var sid =$(this).parents('tr').find('.sid').html()
                                        var url = Config.this_url+'/sales/cost/printing?ids='+sid
                                        window.open(url)
                                    }},
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            $('body').on('mouseenter','.remark',function () {
                if (this.offsetWidth < this.scrollWidth) {
                    var that = this;
                    var text = $(this).text();
                    layer.tips(text, that,{
                        tips: 1,
                        time: 2000         //设置显示时间
                    });
                }
            })
            $('table').on('dblclick','td',function(){

                // if($(this).parents('tr').find('.btn-editone').length >0){
                //     return  false;
                // }

                var sid = $(this).parents('tr').find('.sid').html()
                var url = Config.this_url+'/sales/cost/detail?ids='+sid
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['80%', '90%'],
                    title: "费用单详情",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            });
            $('.btn-addfare').click(function () {
                var url = Config.this_url+'/sales/cost/addfare'
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['80%', '90%'],
                    title: "运费报销费用单",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        addfare: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            $('.joint-search').click(function () {
                var con = $(this).data('con')
                var name = $(this).data('name')
                var url = Config.this_url+'/'+con+'?ids='+$(this).data('id')
                console.log(name)
                Backend.api.addtabs(url, name+'详情', 'fa fa-circle-o fa-fw');
            })


            $('.cancelbill').click(function () {
                var url =  Config.this_url+'/sales/cost/cancelbill'+location.search
                layer.prompt({title: '输入红冲原因', formType: 2}, function(pass, index){
                    if(pass == ''){
                        layer.msg('不能为空');
                        return false;
                    }
                    $.post(url,{reason:pass},function (res) {
                        layer.msg(res.msg)
                        if(res.code == 1){
                            layer.close(index);
                            setTimeout(function () {
                                location.reload();
                            }, 800);
                            window.parent.$(".toolbar .btn-refresh").trigger('click');
                        }
                    })
                });
            })
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});